﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Caixa.BLL;
using Caixa.Model;
using Caixa.Model.Interface;
using Caixa.Model.Enum;
using Caixa.ServiceBinder;
using System.Configuration;
using System.IO;

namespace Caixa
{
	public partial class Frm_Base : Form
	{
		#region " Constructor "

		public Frm_Base()
		{
			InitializeComponent();
		}

		#endregion

		#region " Properts "

		private bool LoadedDadosConta { get; set; }

		#endregion

		#region " Events "

		private void Frm_Base_Load(object sender, EventArgs e)
		{
			SetBackgroudTopo();
			SetLogoTopo();
			SetNumeroBanco();
			LoadDadosConta();
		}

		private void Frm_Base_Activated(object sender, EventArgs e)
		{
			if (!this.LoadedDadosConta)
				LoadDadosConta();
		}

		private void Frm_Base_KeyDown(object sender, KeyEventArgs e)
		{
			if ((!this.Name.ToUpper().Equals("FRM_INICIAL")) && e.KeyCode.Equals(Keys.Escape))
			{
				this.Close();
			}
		}

		#endregion

		#region " Methods "

		private void LoadDadosConta()
		{
			pnlDadosConta.Visible = CaixaEletronicoBLL.IsLogado;
			if (pnlDadosConta.Visible)
			{
				Agencia a = AgenciaBLL.GetByID(CaixaEletronicoBLL.ContaLogada.IDAgencia);
				if (a != null)
					lblAgencia.Text = a.Numero.ToString();
				lblConta.Text = CaixaEletronicoBLL.ContaLogada.Numero.ToString();

				this.LoadedDadosConta = true;
			}
		}

		private void SetBackgroudTopo()
		{
			try
			{
				List<string> corBackgroudTopo = string.Format("{0}", ConfigurationManager.AppSettings.Get("CorBackgroudTopo")).Split(',').ToList();
				if (corBackgroudTopo.Count == 1)
					pnlTopo.BackColor = Color.FromName(corBackgroudTopo[0]);
				else if (corBackgroudTopo.Count == 3)
				{
					int red;
					int.TryParse(corBackgroudTopo[0], out red);

					int green;
					int.TryParse(corBackgroudTopo[1], out green);

					int blue;
					int.TryParse(corBackgroudTopo[2], out blue);

					pnlTopo.BackColor = Color.FromArgb(red, green, blue);
				}
				else
					pnlTopo.BackColor = Color.FromArgb(192, 0, 0);
			}
			catch
			{
				pnlTopo.BackColor = Color.FromArgb(192, 0, 0);
			}
		}

		private void SetLogoTopo()
		{
			try
			{
				string logotipoTopo = string.Format("{0}", ConfigurationManager.AppSettings.Get("LogotipoTopo"));
				imgLogo.ImageLocation = Path.Combine(Application.ExecutablePath.Replace(Path.GetFileName(Application.ExecutablePath), ""), logotipoTopo);
			}
			catch
			{
				imgLogo.ImageLocation = string.Empty;
			}
		}

		private void SetNumeroBanco()
		{
			Banco banco = BancoBLL.GetMySelf();
			if (banco != null)
				lblBanco.Text = string.Format("{0}", banco.Numero);
		}


		#endregion
	}
}
